package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : jiang
 * @date : 2023/10/20 20:47
 */
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result addAddress(@RequestBody AddressBook addressBook){
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 查看当前用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> listAddress(){
        List<AddressBook> addressBookList = addressBookService.listAddress();
        return Result.success(addressBookList);
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        AddressBook addressBook = addressBookService.getDefaultAddress();
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result putDefaultAddress(@RequestBody AddressBook addressBook){
        addressBookService.putDefaultAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result updateAddressById(@RequestBody AddressBook addressBook){
        addressBookService.updateAddressById(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getAddressById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id删除地址
     * @param addressBook
     * @return
     */
    @DeleteMapping
    public Result deleteAddressById(@RequestBody AddressBook addressBook){
        addressBookService.deleteAddressById(addressBook);
        return Result.success();
    }

}
